from PIL import Image, ImageGrab, ImageQt
import cv2
import aircv


# bbox: (left, top, right, bottom)
def grab(save_path, bbox):
    recoImg = ImageGrab.grab(bbox=bbox)
    recoImg.save(save_path)
    return recoImg


# imgTarget: 目标图片
# imgSearch: 搜索的图片
# confidence: 精度
# return: {'result': (211.0, 132.0), 'rectangle': ((左下角), (左上角), (右下角), (右上角)), 'confidence': 0.4220738410949707}
def findImg(imgTarget, imgSearch, confidence=0.3):
    if isinstance(imgTarget, type('a')):
        imgTarget = aircv.imread(imgTarget)

    if isinstance(imgSearch, type('a')):
        imgSearch = aircv.imread(imgSearch)
    return aircv.find_template(imgTarget, imgSearch, confidence)
